package com.ding.mongodb;

import com.ding.mongodb.dao.UserRepository;
import com.ding.mongodb.pojo.Authority;
import com.ding.mongodb.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;

@SpringBootApplication
public class MongodbApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(MongodbApplication.class, args);
        UserRepository repository = context.getBean(UserRepository.class);

        //保存用户
        User user = new User("ding","666");
        user.addAuthority("user");
        user.addAuthority("admin");
        repository.save(user);//使用继承的功能

        //修改用户信息
        repository.updatePassword("ding", "888");//使用混合自定义功能

        //查找用户
        user = repository.findByUsername("ding");//使用@Query自定义查询功能
        System.out.println(user.getPassword());
        List<Authority> authorities = user.getAuthorities();
        for (Authority authority : authorities) {
            System.out.println(authority.getAuthority());
        }

        //删除用户
        repository.deleteByUsername("ding");//使用方法名自定义功能

        /*查询内嵌文档
         
         //创建查询条件
        BasicDBList basicDBList=new BasicDBList();
        basicDBList.add(new BasicDBObject("authorities",Authority.ADMIN))


        Document document=new Document();
        document.put('$and',basicDBList);//采用and查询
        System.out.println(mongoTemplate.findOne(new BasicQuery(document),User).username);*/


        context.close();
    }
}
